package dom4j;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;

import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;

public class XmlDom4j {
	//创建XML
	public void createXmlFile(){
		Document document = DocumentHelper.createDocument();
		Element catalogElement = document.addElement("catalog");
		//addComment加注释的
		catalogElement.addComment("创建一个catalog的XML文件");
		Element journalElement = catalogElement.addElement("journal");
		journalElement.addAttribute("title", "XML Zone");
		journalElement.addAttribute("publisher", "IBM developerWorks");
		
		//在journal中加 article
		Element articleElement = journalElement.addElement("article");
		articleElement.addAttribute("level", "Interdidte");
		articleElement.addAttribute("date", "Deceber-2011");
		
		//在article中加title
		Element titleElement = articleElement.addElement("title");
		//使用setText()给tile加文本
		titleElement.setText("Java XML File 的创建");
		//在article中加author
		Element authorElement = articleElement.addElement("author");
		Element firstElement = authorElement.addElement("firstName");
		firstElement.setText("车");
		Element lastElement = authorElement.addElement("lastName");
		lastElement.setText("文亮");
		
		//catalog Add leading Element
		Element leadingElement = catalogElement.addElement("leadingsoft");
		leadingElement.addAttribute("power", "leadingsoft");
		leadingElement.addAttribute("mail", "mail.leadingsoft.cn");
		Element dempElement = leadingElement.addElement("DEMP");
		dempElement.setText("Demp Program");
		
		try {
			XMLWriter output = new XMLWriter(new FileWriter(new File("catalog.xml")));
			output.write(document);
			output.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
	}
	//读取XML
	public void readXmlFile(){
		SAXReader reader = new SAXReader();
		try {
			Document document = reader.read(new File("catalog.xml"));
			Element rootElement = document.getRootElement();
			parseElement(rootElement);
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	//当不知道XML文件中有什么的话用这种方法,这个迭代很有用
	private static void parseElement(Element e){
		System.out.println(e.getName()+":");

		//遍历子节点 
		for(Iterator iter = e.elementIterator();iter.hasNext();){
			Element element = (Element)iter.next();
			
			//得到节点的属性
			for(Iterator iter1 = element.attributeIterator();iter1.hasNext();){
				Attribute attr = (Attribute)iter1.next();
				if(attr!=null){
					System.out.println(element.getName()+"的属性"+attr.getName()+"的值是:"+attr.getValue());
				}
			}
			
			//得到节点的值 
			if(!element.getText().equals("")){
				System.out.println(element.getName()+"节点的值是:"+element.getText());
			}else{
				System.out.println(element.getName()+"节点没有值");
			}
			
			parseElement(element);
		}
	}
	//当知道节点时取值 
	private static void selectNodes(){
		SAXReader saxReader = new SAXReader();
		try {
			Document document = saxReader.read(new File("catalog.xml"));
			//得到/catalog/journal/下所有的article节点
			List list = document.selectNodes("/catalog/journal/article");
			for(Iterator iter = list.iterator();iter.hasNext();){
				Element element = (Element)iter.next();
				System.out.println(element.getName());
				for(Iterator iter1 = element.attributeIterator();iter1.hasNext();){
					Attribute attr = (Attribute)iter1.next();
					System.out.println(attr.getName()+":"+attr.getValue());
				}
				//parseElement(element);//查找对article下的子元素

			}

		} catch (DocumentException e) {
			e.printStackTrace();
		}
	}
	
	//修改XML
	private static void modifyXML(){
		SAXReader saxReader = new SAXReader();
		Document document;
		try {
			document = saxReader.read(new File("catalog.xml"));
			//找到article下的date属性，并修改属性的值 
			List list1 = document.selectNodes("//article/@date");
			for(Iterator iter11 = list1.iterator();iter11.hasNext();){
				Attribute attr11 = (Attribute)iter11.next();
				System.out.println(attr11.getValue());
				if(attr11.getValue().equals("Deceber-2011")){
					attr11.setValue("2011-11-11 星期五");
				}
				System.out.println(attr11.getValue());
			}	
		} catch (DocumentException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static void main(String[] args) {
		XmlDom4j dom4j = new XmlDom4j();
		//dom4j.createXmlFile();//创建一个XML文件
		//dom4j.readXmlFile();//读取一个XML文件中所有的属性值及text
		//selectNodes();//指定一个节点，获取里面的值
		modifyXML();//修改XML文件里的属性值
	}
}
